package handler

import (
	"api/pkg"
	"github.com/gin-gonic/gin"
	"net/http"
	"path/filepath"
)

func Upload(c *gin.Context) {
	file, err := c.FormFile("file")
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  err,
		})
		return
	}

	ext := filepath.Ext(file.Filename)
	if ext != ".mp4" && ext != ".avi" {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "视频格式错误，请上传为MP4的视频",
		})
		return
	}

	if file.Size > 1000*1024*1024 {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "视频过大，请重新上传",
		})
		return
	}

	upload, err := pkg.Upload(file)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "上传失败",
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "上传成功",
		"url":  upload,
	})
}
